package org.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dk.mapper.TbSpecificationMapper;
import org.dk.mapper.TbSpecificationOptionMapper;
import org.dk.pageInfo.PageInfo2;
import org.dk.pojo.TbSpecification;
import org.dk.pojo.TbSpecificationOption;
import org.dk.pojo.TbSpecificationOptionExample;
import org.dk.service.TbSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class tbSpecificationServiceImpl implements TbSpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    @Override
    public PageInfo2<TbSpecification> select(Integer pageNum, Integer pageSize,TbSpecification tbSpecification) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> list =  tbSpecificationMapper.selectByName(tbSpecification);
        PageInfo<TbSpecification> pageInfo = new PageInfo(list);
        PageInfo2<TbSpecification> pageInfo1 = new PageInfo2();
        pageInfo1.setPageNum(pageInfo.getPageNum());
        pageInfo1.setPageSize(pageInfo.getSize());
        pageInfo1.setTotal(pageInfo.getTotal());
        pageInfo1.setList(pageInfo.getList());
        pageInfo1.setLastPage(pageInfo.getPages());
        return pageInfo1;
    }

    @Override
    public Boolean insertinto(List<TbSpecificationOption> list, TbSpecification specNames) {
        int i = tbSpecificationMapper.insrtByname(specNames);
        Long id = specNames.getId();
        System.out.println(id);
        int insertinto = tbSpecificationOptionMapper.insertinto(list, id);
        return insertinto>0;
    }

    @Override
    public Boolean delsete(Long id) {
        int i = tbSpecificationMapper.deleteByPrimaryKey(id);
        tbSpecificationMapper.deleteByid(id);
        return i>0;
    }

    @Override
    public TbSpecification selectByid(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        return tbSpecification;
    }

    @Override
    public Boolean update(TbSpecification tbSpecification, List<TbSpecificationOption> list) {
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);

        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
        int insertinto = tbSpecificationOptionMapper.insertinto(list, tbSpecification.getId());
        return insertinto>0;
    }

    @Override
    public List<TbSpecification> selectNull() {
        return tbSpecificationMapper.selectByExample(null);
    }
}
